<?php

namespace app\api\controller;

use addons\shopro\model\UserWalletLog;
use app\admin\command\Income;
use app\admin\model\order\OrderBill;
use app\admin\model\share\CommonShare;
use app\admin\model\share\ShareUser;
use app\common\controller\Api;
use app\common\model\User5g;
use GuzzleHttp\Client;
use think\Db;
use app\admin\model\Ad;
use think\Exception;
use think\Request;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *1111
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function banner()
    {
        $list = DB::name('banner')->order('weigh desc')->select();
        foreach ($list as &$v) {
            $v['image'] = cdnurl($v['image'], true);
        }
        $this->success('请求成功', $list);
    }

    public function ad()
    {
        $list = (new Ad)->order('weigh desc')->paginate(1);
        foreach ($list as $v) {
            //  $v['image'] = cdnurl($v['image'],true);
            // dump($v['image']);
            $v->image = cdnurl($v->image, true);

        }
        $this->success('请求成功', $list);
    }

    /**
     * 向未来分析
     * @return void
     */
    public function manager_analysis()
    {
        $num = $this->get_order_sum([25, 26]);

        $num1 = 0;
        $num2 = 0;
        $num3 = 0;
        $num4 = 0;
        $num5 = 0;

        if ($num < 500) {
            $num1 = $num * 2;
        }

        if ($num >= 500 && $num < 1000) {
            $num1 = 1000;
            $num2 = ($num - 500) * 2;
        }

        if ($num >= 1000 && $num < 2000) {
            $num1 = 1000;
            $num2 = 1000;
            $num3 = $num - 1000;
        }

        if ($num >= 2000 && $num < 3000) {
            $num1 = 1000;
            $num2 = 1000;
            $num3 = 1000;
            $num4 = $num - 1000;
        }

        if ($num >= 3000 && $num < 4000) {
            $num1 = 1000;
            $num2 = 1000;
            $num3 = 1000;
            $num4 = 1000;
            $num5 = $num - 1000;
        }

        if ($num >= 4000 && $num < 5000) {
            $num1 = 1000;
            $num2 = 1000;
            $num3 = 1000;
            $num4 = 1000;
            $num5 = 1000;
        }

        $list = [
            1 => $num1,
            2 => $num2,
            3 => $num3,
            4 => $num4,
            5 => $num5,
        ];
        $re_data['list'] = $list;
        $re_data['data'] = $this->get_order_sort(26);

        $this->success('请求成功', $re_data);
    }

    /**
     * 我的市场分析
     * @return void
     */
    public function martet_analysis()
    {
        //$num = $this->get_order_sum([1, 2, 3, 4, 5, 6]) - 1020;
        $num = $this->get_order_sum([1, 2, 3, 4, 5, 6]);
        $num1 = 0;
        $num2 = 0;
        $num3 = 0;
        $num4 = 0;
        $num5 = 0;
        $num6 = 0;

        if ($num < 500) {
            $num1 = $num * 2;
        }

        if ($num >= 500 && $num < 1000) {
            $num1 = 1000;
            $num2 = ($num - 500) * 2;
        }

        if ($num >= 1000 && $num < 2000) {
            $num1 = 1000;
            $num2 = 1000;
            $num3 = $num - 1000;
        }

        if ($num >= 2000 && $num < 3000) {
            $num1 = 1000;
            $num2 = 1000;
            $num3 = 1000;
            $num4 = $num - 1000;
        }

        if ($num >= 3000 && $num < 4000) {
            $num1 = 1000;
            $num2 = 1000;
            $num3 = 1000;
            $num4 = 1000;
            $num5 = $num - 1000;
        }

        if ($num >= 4000 && $num < 5000) {
            $num1 = 1000;
            $num2 = 1000;
            $num3 = 1000;
            $num4 = 1000;
            $num5 = 1000;
            $num6 = $num - 1000;
        }

        $list = [
            1 => $num1,
            2 => $num2,
            3 => $num3,
            4 => $num4,
            5 => $num5,
            6 => $num6,
        ];
        $re_data['list'] = $list;
        $re_data['data'] = $this->get_order_sort(2);
        $this->success('请求成功', $re_data);
    }

    public function get_order_sum($arr = [])
    {
        return 0;
        $re_data = $this->curl_file_get_contents("http://120.78.130.240:8080/dataview-backend-new/api/dataview/getGoodsSaleNum");

        $re_data = json_decode($re_data);
        $sum = 0;
        if(isset($re_data->data)){
            foreach ($re_data->data as $k => $v) {

                if (in_array($v->goodsId, $arr)) {
                    $sum += $v->saleNum;
                }
            }
        }


        return $sum;
    }

    function curl_file_get_contents($durl)
    {

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $durl);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回

        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回

        $data = curl_exec($ch);

        curl_close($ch);

        return $data;

    }

    /**
     * 审核
     *
     * @param $ids
     * @return string
     * @throws \think\Exception
     */
    public function bult_audit()
    {

        $limit = input('limit', 10);
        $token = '4774a99d67c03169d73c9f86a4742474';
        $cur_token = input('token');
        if ($token != $cur_token) {
            echo 'failed';
            return;
        }
        $list = Db::name('user_withdraw')->where('auditStatus', 0)->limit($limit)->lock()->select();

        foreach ($list as $v) {
            $result = false;

            Db::name('user_withdraw')->where('id', $v['id'])->update([
                'auditStatus' => 2,
            ]);

            $item = Db::name('user_withdraw')->where('id', $v['id'])->find();
            Db::startTrans();
            try {
                if ($item['auditStatus'] == \app\admin\model\UserWithdraw::Auditstatus_2) {
                    $shareUserOne = \app\admin\model\share\ShareUser::where('user_id', '=', $item['user_id'])->find();

                    if (isset($shareUserOne->lock_money)) {
                        if ($shareUserOne->lock_money < $item['money']) {
                            $updateParams['payMsg'] = '系统异常：锁定余额' . $shareUserOne->lock_money . '小于提现余额' . $item['money'];
                        } else {
                            $shareUserOne->unlock_money($shareUserOne, $item['money'], $shareUserOne::UNLOCK_MONEY_TRUE,0, 1);
                        }
                    }
                }

                $updateParams['auditStatus'] = $item['auditStatus'];
                $updateParams['auditMsg'] = $item['auditStatus'] != \app\admin\model\UserWithdraw::Auditstatus_2 ? '' : $item['auditMsg'];
                $updateParams['auditUserId'] = 1;
                $updateParams['auditUsername'] = 'admin';
                $updateParams['auditTime'] = time();
                $result = Db::name('user_withdraw')->where('id', $item['id'])->update($updateParams);
                Db::commit();
                echo 'succ' . PHP_EOL;
            } catch (Exception $e1) {
                Db::rollback();

            }
        }
    }
    public function get_order_sort($goodsId)
    {
        $re_data = [];
        return $re_data;
        $get_data = $this->curl_file_get_contents("http://120.78.130.240:8080/dataview-backend-new/api/dataview/getGoodsRank");

        $get_data = json_decode($get_data);
        $sum = 0;
        $re_data = [];
        if(isset($get_data->data)){
            foreach ($get_data->data as $k => $v) {
                if($v->goodsId == $goodsId){
                    /* if ($v->goodsId == 25 || $v->goodsId == 26) {
                         $re_data[$k]['ranking'] = $v->userList[0]->ranking*2;
                     }else{
                         $re_data[$k]['ranking'] = $v->userList[0]->ranking;
                     }*/
                    if($v->userList[0]->ranking < 2000){
                        $re_data[$k]['ranking'] = $v->userList[0]->ranking*2;
                    }
                    $re_data[$k]['phone'] = $this->encryptTel($v->userList[0]->phone);
                    $re_data[$k]['channelSource'] = $v->userList[0]->channelSource;
                    $re_data[$k]['payTime'] = date('Y-m-d H:i:s',$v->userList[0]->payTime);
                }
            }
        }

       return $re_data;
    }
    public static function encryptTel($tel) {
        $new_tel = substr($tel, 0, 3).'****'.substr($tel, 7);
        return $new_tel;
    }

    /**
     * @return void
     * qi10。28
     * 弹窗
     */
    public function tips(){
        if (empty($this->auth->id)) {
            $this->error('请登录后操作');
        }
        $token = \request()->header('token');

        $d_data['token'] = $token;
        $fgurl = Db::name("config")->where(['name'=>'5gurl'])->value('value');
        $g_data = $this->curl_post($fgurl."/api/goods/isRec", $d_data);
        $g_data = json_decode($g_data);

        $re_data = [];
        if($g_data->data->state == 1){
            $user_data = db('user')->where('id',$this->auth->pid)->find();
            //$info = db('notice_user')->where('user_id',$this->auth->id)->where('notice_id',100000001)->find();
            //if (!$info) {
            $re_data['title'] = "领取好物";
            $re_data['content'] = "亲，请点击“确认”，完善收货地址，领取“".$user_data['nickname']."”送给您的好物";
            $re_data['url'] = $g_data->data->link;
            $re_data['id'] = "100000001";
            $re_data['type'] = 2;
            $re_data['status'] = 0;

            //  }
        }else{
            $ll_where['user_id'] = $this->auth->id;
            $ll_where['status'] = ['in', '1,2'];
            $ll_where['pay_fee'] = ['>', 100];
            $ll_where['share_retreat'] = ['neq', 2];
            $ll_data = Db::name("shopro_order")->where($ll_where)->find();
            $re_data['title'] = "";
            $re_data['content'] = "";
            $re_data['url'] = '';
            $re_data['id'] = "0";
            $share_data = Db::name("share_user")->where(['user_id'=>$this->auth->id])->find();
            if(!$ll_data && $share_data['role'] < 2){
                $re_data['type'] = 1;
                $re_data['status'] = 2;
            }else{
                $re_data['type'] = 1;
                $re_data['status'] = 1;
            }
        }

        $this->success('请求成功', $re_data);
    }
    /*
    public function tips(){
        if (empty($this->auth->id)) {
            $this->error('请登录后操作');
        }
        $token = \request()->header('token');

        $d_data['token'] = $token;
        $fgurl = Db::name("config")->where(['name'=>'5gurl'])->value('value');
        $g_data = $this->curl_post($fgurl."/api/goods/isRec", $d_data);
        $g_data = json_decode($g_data);

        $re_data = [];
        if($g_data->data->state == 1){
            $user_data = db('user')->where('id',$this->auth->pid)->find();
            //$info = db('notice_user')->where('user_id',$this->auth->id)->where('notice_id',100000001)->find();
            //if (!$info) {
                $re_data['title'] = "领取好物";
                $re_data['content'] = "亲，请点击“确认”，完善收货地址，领取“".$user_data['nickname']."”送给您的好物";
                $re_data['url'] = $g_data->data->link;
                $re_data['id'] = "100000001";

                $re_data['type'] = 2;
                $re_data['status'] = 0;
          //  }
        }

        $this->success('请求成功', $re_data);
    }*/
   /* public function tips(){
        if (empty($this->auth->id)) {
            $this->error('请登录后操作');
        }
        $re_data = [];
        $info = db('shopro_order')->where('df_user_id',$this->auth->id)->where('status',0)->where('df_reject',0)->where('is_df',1)->field('id,order_sn,goods_id,user_id,df_remark')->find();
        if ($info) {
            $nickname = db('user')->where('id',$info['user_id'])->value('nickname');
            $re_data['title'] = "代付请求";
            $re_data['content'] = "亲，您有一个来自".$nickname."的代付订单，确认帮他付款吗？";
            $re_data['url'] = "qrCodePay/".$info['order_sn'];
            $re_data['id'] = $info['id'];
        }
        $this->success('请求成功', $re_data);
    }
    public function tips_cancel(){
        $id = $this->request->post("id");
        db('shopro_order')->where('id',$id)->update(['df_reject'=>1]);
        $this->success('请求成功');
    }*/
    public function tips_cancel(){
        $id = $this->request->post("id");

        db('notice_user')->insert(['user_id'=>$this->auth->id,'notice_id'=>$id,'createtime'=>time()]);
        $this->success('请求成功');
    }
    // 广告接口
    public function adList()
    {
        $list = db('ad')->order('weigh desc')->select();
        $picArr = [];
        $videoArr = [];
        foreach ($list as $v) {
            if (in_array(explode('.',$v['image'])[1],['mp4','avi','wmv'])) {
                $videoArr[] = ['id'=>$v['id'],'url'=>cdnurl($v['image'])];
            } else {
                $picArr[] = ['id'=>$v['id'],'url'=>cdnurl($v['image'])];
            }
        }
        $this->success('请求成功',['pic'=>$picArr,'video'=>$videoArr]);
    }

    // 广告评论(发送)
    public function ad_comment()
    {
        if (empty($this->auth->id)) {
            $this->error('请登录后操作');
        }
        $data = [];
        $data['a_id'] = input('a_id'); // 广告id
        $data['c_id'] = input('c_id') ? input('c_id') : 0; // 评论id
        $data['user_id'] = $this->auth->id;
        $data['content'] = input('content');
        $data['ip'] = request()->ip();
        $data['ip'] = '112.32.126.104';

        if ($data['c_id']) {
            // 查询评论id是否存在
            $comment = db('ad_comment')->where('id',$data['c_id'])->find();
            if (!$comment) {
                $this->error('评论id不存在');
            }
        }

        // 太平洋电脑网接口
        $url = "http://whois.pconline.com.cn/ipJson.jsp?ip={$data['ip']}&json=true";
        $client = new Client();
        $response = $client->get($url);
        $body = $response->getBody()->getContents();
        $encoding = mb_detect_encoding($body, ['utf-8', 'gbk', 'latin1', 'big5']);
        if ($encoding !== 'utf-8') {
            $body = mb_convert_encoding($body, 'utf-8', $encoding);
        }
        $body = json_decode(trim($body), true);
        if (empty($body['pro'])) {// 获取不到省份
            $this->error(__('Unable to obtain IP, please contact customer service'), null, 2);
        }
        $data['city'] = $body['city'];
        $data['createtime'] = date('Y-m-d H:i:s',time());
        $ins = db('ad_comment')->insert($data);
        if ($ins) {
            $this->success('评论成功');
        }
        $this->error('评论失败');

    }


    // 评论列表
    public function comment_list()
    {
        $id = input('id');
        $list = db('ad_comment')->where('a_id',$id)->where('c_id',0)->select();
        foreach ($list as &$v) {
            $v['nickname'] = db('user')->where('id',$v['user_id'])->value('nickname');
            // 点赞次数
            $v['star'] = db('ad_comment_star')->where('c_id',$v['id'])->count();

            // 我有没有点赞
            $v['is_red'] = db('ad_comment_star')->where('c_id',$v['id'])->where('user_id',$this->auth->id)->count();
            $nextComment = db('ad_comment')->where('c_id',$v['id'])->select();
            foreach ($nextComment as &$vv) {
                $vv['nickname'] = db('user')->where('id',$vv['user_id'])->value('nickname');
                // 点赞次数
                $vv['star'] = db('ad_comment_star')->where('c_id',$vv['id'])->count();

                // 我有没有点赞
                $vv['is_red'] = db('ad_comment_star')->where('c_id',$vv['id'])->where('user_id',$this->auth->id)->count();
            }
            $v['next_comment'] = $nextComment;
        }
        $this->success('成功',$list);

    }

    // 点赞
    public function comment_start()
    {
        if (empty($this->auth->id)) {
            $this->error('请登录后操作');
        }
        $cid = input('c_id');
        $s = db('ad_comment_star')->where('user_id',$this->auth->id)->where('c_id',$cid)->find();
        if ($s) {
            db('ad_comment_star')->where('id',$s['id'])->delete();
            $this->success('取消点赞成功');
        } else {
            db('ad_comment_star')->insert(['user_id'=>$this->auth->id,'c_id'=>$cid,'createtime'=>date('Y-m-d H:i:s',time())]);
            $this->success('点赞成功');
        }
    }

    /**
     * 代付超时自动取消
     */
    public function cancalDyOrder()
    {
        $list = db('shopro_order')->where('is_df',1)->where('status',0)->field('id,order_sn,createtime,status,goods_id')->select();
        foreach ($list as $item) {
            $diff = time() - $item['createtime']; // 下单已过去的秒
            if ($diff > 1200) { // 20分钟自动取消
                $up = db('shopro_order')->where('id',$item['id'])->update(['status'=>-1]);
                if ($up) {
                    db("shopro_goods_sku_price")->where(["goods_id" => $item['goods_id']])->setInc('stock', 1);
                    echo '订单号'.$item['order_sn'].'自动取消成功'.'<br/>';
                }
            }
        }
        echo '执行完成';
    }

    /**
     * 申请独立负责人
     */
    public function dependent_apply()
    {
        if (empty($this->auth->id)) {
            $this->error('请登录后操作');
        }
        $info = db('dependent')->where('user_id',$this->auth->id)->find();
        if ($info) {
            $this->error('重复申请操作');
        }
        db('dependent')->insert(['user_id'=>$this->auth->id,'createtime'=>date('Y-m-d H:i:s',time())]);
        $this->success('提交申请成功');
    }
    /**
     * 余额变动
     */
    public function lp_now(){
        $list = Db::name("share_user")->where('price','>',0)->select();
        foreach ($list as $k=>$v){
            if($v['price'] > 0){
                $m = $v['price'] + $v['lp_price'];
                Db::name("share_user")->where(['id'=>$v['id']])->update(['price'=>0,'lp_price'=>$m]);
                //echo  Db::name("share_user")->getLastSql();
            }
        }
        Db::name("user_withdraw")->where(['with_type'=>0])->update(['with_type'=>3]);
        Db::name("user_money_log")->where(['is_old'=>0])->update(['is_old'=>2]);
        Db::name("shopro_order")->where('id','>',0)->update(['is_bill'=>1]);
        echo "成功";
    }

    /**
     * 手动发放收益
     */
    public function income_money(){
        $url = $_SERVER['HTTP_HOST'];
        if($url != 'zhongjunf.bigchun.com' && $url != 'y-sphj.bigchun.com'){
            echo "无权执行";
            exit;
        }
        (new Income())->CeshiIncome();
        echo "成功";
    }
    /**
     * 手动发放零售批发收益
     */
    public function income_lp_money(){
        $url = $_SERVER['HTTP_HOST'];
        if($url != 'zhongjunf.bigchun.com' && $url != 'y-sphj.bigchun.com'){
            echo "无权执行";
            exit;
        }
        (new Income())->lpIncome();
        echo "成功";
    }
    /**
     * 独立负责人申请状态
     */
    public function dependent_apply_status()
    {
        if (empty($this->auth->id)) {
            $this->error('请登录后操作');
        }
        $info = db('leader_user')->where('user_id', $this->auth->id)->find();
        if ($info) {
            $this->success('ok', $info);
        }
        $this->error('信息不存在');
    }

    /**
     * 独立负责人排行榜
     */
    public function sep_income_rank()
    {
//        今日、本周、本月、季度、年、时间自选
        $type = input('type'); // 类型1=业绩,2=单数
        $timeType = input('timeType');

        $where = [];
        $whereOrder = [];
        $whereOrder['status'] = ['in',[1,2]];
        $whereOrder['share_retreat'] = 0;
        $whereOrder['one_day'] = 5;
        $where['id'] = ['>',0];
        if ($timeType == 1) {
            // 今日
            $start_time = strtotime(date('Y-m-d').'00:00:00');
            $end_time = strtotime(date('Y-m-d').'23:59:59');
        } elseif($timeType == 2) {
            // 本周
            $start_time = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));
            $end_time = mktime(23,59,59,date('m'),date('d')-date('w')+7,date('y'));
        } elseif ($timeType == 3) {
            // 本月
            $start_time =mktime(0,0,0,date('m'),1,date('Y'));
            $end_time =mktime(23,59,59,date('m'),date('t'),date('Y'));
        } elseif ($timeType == 4) {
            // 季度
            $season = ceil((date('m'))/3);
            $start_time = mktime(00,00,00,$season*2+1,1,date('Y'));
            $end_time = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));

        } elseif ($timeType == 5) {
            // 年
            $start_time  = strtotime(date("Y",time())."-1"."-1");
            $end_time  = strtotime(date("Y",time())."-12"."-31");
        } elseif ($timeType == 6) {
            // 自定义
            $start_time = strtotime(input('start_time') .' 00:00:00');
            $end_time = strtotime(input('end_time').' 23:59:59');
        } else{
            $start_time = '';
            $end_time = '';
        }




        if ($start_time && $end_time) {
//            $where['addtime'] = ['between',[$start_time,$end_time]];
            $whereOrder['createtime'] = ['between',[$start_time,$end_time]];
        }

        $userList = db('share_user')->where('is_sep_income',1)->field('id,user_id,user_flags')->select();

        foreach ($userList as $k => &$v) {
            $user = db('user')->where('id',$v['user_id'])->field('id,nickname,mobile')->find();
            $v['nickname'] = $user['nickname'];
            $v['mobile'] = $user['mobile'];
            // 人数

            $flagArr = db('share_user_flags')->where('user_flags',$v['user_flags'])->where($where)->select();
            $v['count'] = count($flagArr);
            $userArr = array_unique(array_column($flagArr,'user_id'));

            // 单数
            $order = db('shopro_order')->where('user_id','in',$userArr)->where($whereOrder)->field('total_amount')->select();
            $v['order_num'] = count($order);
            // 业绩
            $v['order_money'] = round(array_sum(array_column($order,'total_amount')));
            unset($v['user_id']);
            unset($v['user_flags']);
        }
        if ($type == 2) {
            // 单数
            array_multisort(array_column($userList,'order_num'),SORT_DESC,$userList);
        } elseif ($type == 1) {
            // 业绩
            array_multisort(array_column($userList,'order_money'),SORT_DESC,$userList);
        } else {
            // 人数
            array_multisort(array_column($userList,'count'),SORT_DESC,$userList);
        }

        foreach ($userList as $kk => $vv) {
            if ($type == 2) {
                // 单数
                if ($userList[0]['order_num'] == 0) {
                    $userList[$kk]['percent'] = 0;
                } else {
                    $userList[$kk]['percent'] = ceil(($vv['order_num'] / $userList[0]['order_num']) * 100);
                }
            } elseif ($type == 1) {
                // 业绩
                if ($userList[0]['order_money'] == 0) {
                    $userList[$kk]['percent'] = 0;
                } else {
                    $userList[$kk]['percent'] = ceil(($vv['order_money'] / $userList[0]['order_money']) * 100);
                }
            } else {
                // 人数
                if ($userList[0]['count'] == 0) {
                    $userList[$kk]['percent'] = 0;
                } else {
                    $userList[$kk]['percent'] = ceil(($vv['count'] / $userList[0]['count']) * 100);
                }

            }
        }
        $this->success('ok', $userList);

    }


    /**
     * @return 业绩排行榜
     */
    public function sep_income_rank2()
    {
        if (empty($this->auth->id)) {
            $this->error('请登录后操作');
        }
        // 今日、本周、本月、季度、年、时间自选
        $type = input('type'); // 类型1=业绩,2=单数
        $timeType = input('timeType');
        $operType = input('operType');

        if (empty($type)) {
            $type = 2;
        }
        $role = input('role'); // 角色
        if ($timeType == 1) {
            // 今日
            $start_time = strtotime(date('Y-m-d').'00:00:00');
            $end_time = strtotime(date('Y-m-d').'23:59:59');
        } elseif($timeType == 2) {
            // 本周
            $start_time = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));
            $end_time = mktime(23,59,59,date('m'),date('d')-date('w')+7,date('y'));
        } elseif ($timeType == 3) {
            // 本月
            $start_time =mktime(0,0,0,date('m'),1,date('Y'));
            $end_time =mktime(23,59,59,date('m'),date('t'),date('Y'));
        } elseif ($timeType == 4) {
            // 季度
            $season = ceil((date('m'))/3);
            $start_time = mktime(00,00,00,$season*2+1,1,date('Y'));
            $end_time = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));

        } elseif ($timeType == 5) {
            // 年
            $start_time  = strtotime(date("Y",time())."-1"."-1");
            $end_time  = strtotime(date("Y",time())."-12"."-31");
        } elseif ($timeType == 6) {
            // 自定义
            $start_time = strtotime(input('start_time') .' 00:00:00');
            $end_time = strtotime(input('end_time').' 23:59:59');
        } else{
            $start_time = '';
            $end_time = '';
        }
        $whereOrder = [];
        $whereOrder['status'] = ['in',[1,2]];
        $whereOrder['share_retreat'] = 0;
        $whereOrder['one_day'] = 5;

        $user_flags = db('share_user')->where('user_id',$this->auth->id)->find();
        if ($user_flags['is_leader'] == 1 && $operType == 1) {
            $userListArr = db('share_user')->column('user_id');
        } elseif ($user_flags['is_sep_income'] == 1 && $operType == 2) {
            $userListArr = db('share_user_flags')->where('user_flags',$user_flags['user_flags'])->column('user_id');
        } else {
            $userListArr = [];
        }
        if ($start_time && $end_time) {
            $whereOrder['createtime'] = ['between',[$start_time,$end_time]];
        }

        $whereOrder['user_id'] = ['in',$userListArr];
        $userList = db('shopro_order')->group('user_id')->where($whereOrder)->field('user_id,total_amount,sum(total_amount) as order_money,count(*) as order_num')->select();
        foreach ($userList as $k => &$v) {
            if ($role) {
                $userRole = db('share_user')->where('user_id',$v['user_id'])->value('role');
                if ($userRole != $role) {
                    unset($userList[$k]);
                }
            }
            $user = db('user')->where('id',$v['user_id'])->field('id,nickname,mobile')->find();
            $v['nickname'] = $user['nickname'];
            $v['mobile'] = $user['mobile'];
            unset($v['user_id']);
            unset($v['user_flags']);
            unset($v['total_amount']);

        }
        if ($type == 2) {
            // 单数
            array_multisort(array_column($userList,'order_num'),SORT_DESC,$userList);
        } elseif ($type == 1) {
            // 业绩
            array_multisort(array_column($userList,'order_money'),SORT_DESC,$userList);
        }

        foreach ($userList as $kk => $vv) {
            if ($type == 2) {
                // 单数
                if ($userList[0]['order_num'] == 0) {
                    $userList[$kk]['percent'] = 0;
                } else {
                    $userList[$kk]['percent'] = ceil(($vv['order_num'] / $userList[0]['order_num']) * 100);
                }
            } elseif ($type == 1) {
                // 业绩
                if ($userList[0]['order_money'] == 0) {
                    $userList[$kk]['percent'] = 0;
                } else {
                    $userList[$kk]['percent'] = ceil(($vv['order_money'] / $userList[0]['order_money']) * 100);
                }
            }
        }

        if (isset(array_chunk($userList,10)[0])) {
            $userList = array_chunk($userList,10)[0];
        } else {
            $userList = [];
        }

        $this->success('ok', $userList);
    }

    /**
     * 角色列表
     */
    public function roleList()
    {
//        全部、银牌、二星网店、一星网点、代言人、推广员
        $arr = [
            ['id'=>0,'name'=>'全部'],
            ['id'=>5,'name'=>'银牌'],
            ['id'=>4,'name'=>'二星网店'],
            ['id'=>3,'name'=>'一星网店'],
            ['id'=>2,'name'=>'代言人'],
            ['id'=>1,'name'=>'推广员'],
        ];
        $this->success('ok', $arr);

    }


    /**
     * 分账入库
     * @return void
     */
    public function get_teams_fee()
    {
        set_time_limit(0);
        $typeD = input('type',0); // 默认查询当天的订单，传all查询当月订单
        $typeArr = [5,2,1]; //type : 5=品牌专区 2=易购臻品 1=每月一品
        foreach ($typeArr as $type) {
                //type类型下的当日总业绩和订单数
                $fields = [
                    'goods_amount as order_price',
                    'order_sn',
                    'platform',
                    'user_id',
                    'createtime',
                ];
                $config = Db::table('shopro_goods')->where('one_day', '=', $type)->column('id');
                $where = [];
                $where['goods_id'] = ['in',$config];
                $where['status'] = ['>=',1];
                $where['share_retreat'] = 0;
                if ($typeD) {
                    // 本月的订单
                    $where['createtime'] = ['>=',date("Y-m-d H:i:s",mktime (0,0,0, date("m"),1,date("Y")))];
                } else {
                    // 今日订单
                    $where['createtime'] = ['>=',mktime(0,0,0,date('m'),date('d'),date('Y'))];
                }

                $yu_account = Db::table('shopro_order')->field($fields)->where($where)->select();
                foreach ($yu_account as $orderItem) {
                    if ($type == 5) {
                        $rate = 0.329;
                    } elseif ($type == 1) {
                        $rate = 0.29;
                    } else {
                        $rate = 0.07;
                    }
                    $today_sub_account = round($orderItem['order_price'] * $rate, 2);

                    //type类型下的当日总业绩和订单数
                    $all_account = $orderItem['order_price'];

                    //分公司
                    $fgs = round($all_account * 0.329, 2);//分公司总金额
                    //总公司
                    $z_fgs = round($all_account * 0.38, 2);//总公司总金额
                    //第三方
                    $d_fgs = round($all_account * 0.291, 2);//第三方公司总金额

                    if ($orderItem['platform'] == 'Coupon') {
                        $ti_account = $all_account;
                        $yu_account = 0;
                    } else {
                        $ti_account = 0;
                        $yu_account = $all_account;
                    }

                    if ($type == 5) {
                        $arr = [
                            "name" => "分公司分账：（32.9%）",
                            "amount" => $fgs,
                            "list" => [
                                0 => [
                                    'name' => '零批创收分账（23%）',
                                    'rate' => 23,
                                    'amount' => round(($all_account * 0.23), 2),
                                    'ti_amount' => round(($ti_account * 0.23), 2),
                                    'yu_amount' => round(($yu_account * 0.23), 2),
                                ],
                                1 => [
                                    'name' => '分公司联创分账（3%）',
                                    'rate' => 3,
                                    'amount' => round(($all_account * 0.03), 2),
                                    'ti_amount' => round(($ti_account * 0.03), 2),
                                    'yu_amount' => round(($yu_account * 0.03), 2),
                                ],
                                2 => [
                                    'name' => '分公司团队运营（2%）',
                                    'rate' => 2,
                                    'amount' => round(($all_account * 0.02), 2),
                                    'ti_amount' => round(($ti_account * 0.02), 2),
                                    'yu_amount' => round(($yu_account * 0.02), 2),
                                ],
                                3 => [
                                    'name' => '分公司股东分账（4.9%）',
                                    'rate' => 4.9,
                                    'amount' => round(($all_account * 0.049), 2),
                                    'ti_amount' => round(($ti_account * 0.049), 2),
                                    'yu_amount' => round(($yu_account * 0.049), 2),
                                ],
                            ],
                            "name1" => "总公司分账：（38%）",
                            "amount1" => $z_fgs,
                            "list1" => [
                                0 => [
                                    'name' => '供应商分账（35%）',
                                    'rate' => 35,
                                    'amount' => round(($all_account * 0.35), 2),
                                    'ti_amount' => round(($ti_account * 0.35), 2),
                                    'yu_amount' => round(($yu_account * 0.35), 2),
                                ],
                                1 => [
                                    'name' => '总公司平台技术费（2%）',
                                    'rate' => 2,
                                    'amount' => round(($all_account * 0.02), 2),
                                    'ti_amount' => round(($ti_account * 0.02), 2),
                                    'yu_amount' => round(($yu_account * 0.02), 2),
                                ],
                                2 => [
                                    'name' => '总公司综合运营费（1%）',
                                    'rate' => 1,
                                    'amount' => round(($all_account * 0.01), 2),
                                    'ti_amount' => round(($ti_account * 0.01), 2),
                                    'yu_amount' => round(($yu_account * 0.01), 2),
                                ],

                            ],
                            "name2" => "第三方广告公司：（29.1%）",
                            "amount2" => $d_fgs,
                            "list2" => [
                                0 => [
                                    'name' => '广告创收分账（26.1%+3%）',
                                    'rate' => 29.1,
                                    'amount' => round(($all_account * 0.291), 2),
                                    'ti_amount' => round(($ti_account * 0.291), 2),
                                    'yu_amount' => round(($yu_account * 0.291), 2),
                                ],
                            ],

                        ];

                    } elseif ($type == 1) {
                        $arr = [
                            "name" => "分公司分账：（29%）",
                            "amount" => round(($all_account * 0.29), 2),
                            "list" => [
                                0 => [
                                    'name' => '分公司股东分账（27%）',
                                    'rate' => 27,
                                    'amount' => round(($all_account * 0.27), 2),
                                    'ti_amount' => round(($ti_account * 0.27), 2),
                                    'yu_amount' => round(($yu_account * 0.27), 2),
                                ],
                                1 => [
                                    'name' => '分公司团队运营（2%）',
                                    'rate' => 2,
                                    'amount' => round(($all_account * 0.02), 2),
                                    'ti_amount' => round(($ti_account * 0.02), 2),
                                    'yu_amount' => round(($yu_account * 0.02), 2),
                                ],

                            ],
                            "name1" => "总公司分账：（38%）",
                            "amount1" => $z_fgs,
                            "list1" => [
                                0 => [
                                    'name' => '供应商分账（35%）',
                                    'rate' => 35,
                                    'amount' => round(($all_account * 0.35), 2),
                                    'ti_amount' => round(($ti_account * 0.35), 2),
                                    'yu_amount' => round(($yu_account * 0.35), 2),
                                ],
                                1 => [
                                    'name' => '总公司平台技术费（2%）',
                                    'rate' => 2,
                                    'amount' => round(($all_account * 0.02), 2),
                                    'ti_amount' => round(($ti_account * 0.02), 2),
                                    'yu_amount' => round(($yu_account * 0.02), 2),
                                ],
                                2 => [
                                    'name' => '总公司综合运营费（1%）',
                                    'rate' => 1,
                                    'amount' => round(($all_account * 0.01), 2),
                                    'ti_amount' => round(($ti_account * 0.01), 2),
                                    'yu_amount' => round(($yu_account * 0.01), 2),
                                ],

                            ],
                            "name2" => "第三方广告公司：（33%）",
                            "amount2" => round(($all_account * 0.33), 2),
                            "list2" => [
                                0 => [
                                    'name' => '广告创收分账（30%+3%）',
                                    'rate' => 33,
                                    'amount' => round(($all_account * 0.33), 2),
                                    'ti_amount' => round(($ti_account * 0.33), 2),
                                    'yu_amount' => round(($yu_account * 0.33), 2),
                                ],
                            ],

                        ];
                    } elseif ($type == 2) {
                        $arr = [
                            "name" => "分公司分账：（7%）",
                            "amount" => round(($all_account * 0.07), 2),
                            "list" => [
                                0 => [
                                    'name' => '分公司股东分账（5%）',
                                    'rate' => 5,
                                    'amount' => round(($all_account * 0.05), 2),
                                    'ti_amount' => round(($ti_account * 0.05), 2),
                                    'yu_amount' => round(($yu_account * 0.05), 2),
                                ],
                                1 => [
                                    'name' => '分公司团队运营（2%）',
                                    'rate' => 2,
                                    'amount' => round(($all_account * 0.02), 2),
                                    'ti_amount' => round(($ti_account * 0.02), 2),
                                    'yu_amount' => round(($yu_account * 0.02), 2),
                                ],

                            ],
                            "name1" => "总公司分账：（60%）",
                            "amount1" => round(($all_account * 0.6), 2),
                            "list1" => [
                                0 => [
                                    'name' => '易购甄选运营公司（57%）',
                                    'rate' => 57,
                                    'amount' => round(($all_account * 0.57), 2),
                                    'ti_amount' => round(($ti_account * 0.57), 2),
                                    'yu_amount' => round(($yu_account * 0.57), 2),
                                ],
                                1 => [
                                    'name' => '总公司平台技术费（2%）',
                                    'rate' => 2,
                                    'amount' => round(($all_account * 0.02), 2),
                                    'ti_amount' => round(($ti_account * 0.02), 2),
                                    'yu_amount' => round(($yu_account * 0.02), 2),
                                ],
                                2 => [
                                    'name' => '总公司综合运营费（1%）',
                                    'rate' => 1,
                                    'amount' => round(($all_account * 0.01), 2),
                                    'ti_amount' => round(($ti_account * 0.01), 2),
                                    'yu_amount' => round(($yu_account * 0.01), 2),
                                ],

                            ],
                            "name2" => "第三方广告公司：（33%）",
                            "amount2" => round(($all_account * 0.33), 2),
                            "list2" => [
                                0 => [
                                    'name' => '广告创收分账（30%+3%）',
                                    'rate' => 33,
                                    'amount' => round(($all_account * 0.33), 2),
                                    'ti_amount' => round(($ti_account * 0.33), 2),
                                    'yu_amount' => round(($yu_account * 0.33), 2),
                                ],
                            ],

                        ];
                    }

                    $ins = [];
                    $ins['percent_fgs'] = 0.329;
                    $ins['percent_zgs'] = 0.38;
                    $ins['percent_dsf'] = 0.291;

                    $ins['fgs_money'] = $fgs;
                    $ins['zgs_money'] = $z_fgs;
                    $ins['dsf_money'] = $d_fgs;


                    $ins['user_id'] = $orderItem['user_id'];
                    $ins['order_sn'] = $orderItem['order_sn'];
                    $ins['total_amount'] = $orderItem['order_price'];
                    $ins['status'] = 0;
                    if ($orderItem['platform'] == 'Coupon') {
                        $ins['type'] = 1;
                    } else {
                        $ins['type'] = 0;
                        $ins['status'] = 1; // 钱包不用审核
                        $ins['fz_time'] = time(); // 分账时间
                    }
                    $ins['all_account'] = $all_account;
                    $ins['all_order'] = 1;
                    $ins['today_sub_account'] = $today_sub_account;
                    $ins['goods_type'] = $type;
                    $ins['sublist'] = json_encode($arr,JSON_UNESCAPED_UNICODE);
                    $ins['createtime'] = $orderItem['createtime'];

                    $info = db('shopro_order_bill')->where('order_sn',$ins['order_sn'])->where('user_id',$orderItem['user_id'])->where('goods_type',$type)->where('api_list',0)->find();
                    if (!$info) {
                        $op = db('shopro_order_bill')->insert($ins);
                        if ($op) {
                            $fzArr = [];
                            foreach ($arr as $fzDetail) {
                                if (is_array($fzDetail)) {
                                    foreach ($fzDetail as $fz_dd) {
                                        $ins['percent'] = $fz_dd['rate'];
                                        $ins['fz_title'] = $fz_dd['name'];
                                        $ins['fz_money'] = $fz_dd['amount'];
                                        $ins['api_list'] = 1;
                                        $fzArr[] = $ins;
                                    }
                                }
                            }
                            db('shopro_order_bill')->insertAll($ins);
                        }
                    }
                }
        }

        echo 'success';
    }

    // 分账表读取数据
    public function bill()
    {
        $type = input('type',5);
        $month_kpi = db('shopro_order_bill')->whereTime('createtime','month')->where('api_list',0)->sum('all_account');
        $yes_kpi = db('shopro_order_bill')->whereTime('createtime','yesterday')->where('api_list',0)->sum('all_account');
        $today_kpi = db('shopro_order_bill')->whereTime('createtime','today')->where('api_list',0)->sum('all_account');

        $month_sub_account = db('shopro_order_bill')->whereTime('createtime','month')->where('api_list',0)->sum('today_sub_account');
        $yes_sub_account = db('shopro_order_bill')->whereTime('createtime','yesterday')->where('api_list',0)->sum('today_sub_account');
        $today_sub_account = db('shopro_order_bill')->whereTime('createtime','today')->where('api_list',0)->sum('today_sub_account');

        $all_account = db('shopro_order_bill')->where('goods_type',$type)->whereTime('createtime','today')->where('api_list',0)->sum('all_account');
        $all_order = db('shopro_order_bill')->where('goods_type',$type)->whereTime('createtime','today')->where('api_list',0)->count();
        $arr = db('shopro_order_bill')->where('goods_type',$type)->whereTime('createtime','today')->where('api_list',0)->value('sublist');

        if (!$arr) {
            if ($type == 5) {
                $arr = [
                    "name" => "分公司分账：（32.9%）",
                    "amount" => 0,
                    "list" => [
                        0 => [
                            'name' => '零批创收分账（23%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                        1 => [
                            'name' => '分公司联创分账（3%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                        2 => [
                            'name' => '分公司团队运营（2%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                        3 => [
                            'name' => '分公司股东分账（4.9%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                    ],
                    "name1" => "总公司分账：（38%）",
                    "amount1" => 0,
                    "list1" => [
                        0 => [
                            'name' => '供应商分账（35%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                        1 => [
                            'name' => '总公司平台技术费（2%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                        2 => [
                            'name' => '总公司综合运营费（1%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],

                    ],
                    "name2" => "第三方广告公司：（29.1%）",
                    "amount2" => 0,
                    "list2" => [
                        0 => [
                            'name' => '广告创收分账（26.1%+3%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                    ],

                ];
            } elseif ($type == 1) {
                $arr = [
                    "name" => "分公司分账：（29%）",
                    "amount" => 0,
                    "list" => [
                        0 => [
                            'name' => '分公司股东分账（27%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                        1 => [
                            'name' => '分公司团队运营（2%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],

                    ],
                    "name1" => "总公司分账：（38%）",
                    "amount1" => 0,
                    "list1" => [
                        0 => [
                            'name' => '供应商分账（35%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                        1 => [
                            'name' => '总公司平台技术费（2%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                        2 => [
                            'name' => '总公司综合运营费（1%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],

                    ],
                    "name2" => "第三方广告公司：（33%）",
                    "amount2" => 0,
                    "list2" => [
                        0 => [
                            'name' => '广告创收分账（30%+3%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                    ],

                ];
            } elseif ($type == 2) {
                $arr = [
                    "name" => "分公司分账：（7%）",
                    "amount" => 0,
                    "list" => [
                        0 => [
                            'name' => '分公司股东分账（5%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                        1 => [
                            'name' => '分公司团队运营（2%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],

                    ],
                    "name1" => "总公司分账：（60%）",
                    "amount1" => 0,
                    "list1" => [
                        0 => [
                            'name' => '易购甄选运营公司（57%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                        1 => [
                            'name' => '总公司平台技术费（2%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                        2 => [
                            'name' => '总公司综合运营费（1%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],

                    ],
                    "name2" => "第三方广告公司：（33%）",
                    "amount2" => 0,
                    "list2" => [
                        0 => [
                            'name' => '广告创收分账（30%+3%）',
                            'amount' => 0,
                            'ti_amount' => 0,
                            'yu_amount' => 0,
                        ],
                    ],

                ];
            }
        } else {
            $arr = json_decode($arr);
        }
        $this->success('成功', [
            'all_account' => $all_account,
            'all_order' => $all_order,
            'month_kpi' => $month_kpi,
            'month_sub_account' => $month_sub_account,
            'sub_list' => $arr,
            'today_kpi' => $today_kpi,
            'today_sub_account' => $today_sub_account,
            'yes_kpi' => $yes_kpi,
            'yes_sub_account' => $yes_sub_account,
        ]);
    }
    public function ceshi()
    {
        (new CommonShare())->reUserDate();
    }

    public function ceshi1()
    {
        (new CommonShare())->reOtherDate();
    }

    public function ruiMoney(){

        $d = Db::name("shopro_order_bill")->field("sum(total_amount) as total_amount_sum,sum(fz_money) as fz_money_sum")->whereTime('createtime', 'today')->find();
        var_dump($d);
    }

    public function ttt()
    {
        echo 2;
    }
    public function ceshif(){
        $token = '4774a99d67c03169d73c9f86a4742474';
        $cur_token = input('token');
        if ($token != $cur_token) {
            echo 'failed';
            return;
        }
        (new Income())->userFriends();
        echo "成功";
    }
    public function order_tui(){
        $token = '4774a99d67c03169d73c9f86a4742474';
        $cur_token = input('token');
        if ($token != $cur_token) {
            echo 'failed11';
            return;
        }
        $sql = "SELECT  sum(money) as order_lp_mpney,sum(mutual_money) as sum_mutual_money,user_id FROM share_money WHERE type in (1,2) and  order_id in ( select order_id from share_reward where order_id not in (select id from shopro_order where `status` > 0 and share_retreat = 0) ) GROUP BY user_id";
        $model = Db::connect();
        $data = $model->query($sql);
        if($data){

            foreach($data as $k=>$v){
                $share_data = Db::name("share_user")->where(['user_id'=>$v['user_id']])->find();
                //余额
                /*if($share_data['lp_price'] > $v['order_lp_mpney']){
                    $k = $v['order_lp_mpney'];
                }else{
                    $k = $share_data['lp_price'];
                }
                (new ShareUser)->userPriceLog($v['user_id'], -$k, 10, $memo = '订单不存在退零批收益','',['is_old'=>2]);*/
                //互助金
                if($share_data['mutual_money'] > $v['sum_mutual_money']){
                    $mutual_k = $v['sum_mutual_money'];
                }else{
                    $mutual_k = $share_data['mutual_money'];
                }
                if($mutual_k > 0){

                    $after = $share_data['mutual_money'] - $mutual_k;
                    Db::name("share_user")->where(["user_id"=>$v['user_id']])->update(['mutual_money'=>$after]);
                    UserWalletLog::create(['user_id' => $v['user_id'], 'wallet' => -$mutual_k, 'wallet_type' => 'mutual', 'type' => 'mutual_add', 'before' => $share_data['mutual_money'], 'after' => $after, 'item_id' => 0, 'memo' => '订单不存在退单余额减少', 'oper_type' => 'system', 'createtime' => time()]);
                }
            }
        }
        echo "成功";

    }
    public function re_pass(){
        $token = '4774a99d67c03sdfwsd73c9f86a4ddd74';
        $cur_token = input('token');
        if ($token != $cur_token) {
            echo 'failed11';
            return;
        }
        $list = Db::name("user")->select();
        foreach ($list as $k=>$v){
            $pa_data = Db::name("pay_passwd")->where(['user_id'=>$v['id']])->find();
            $in_data['user_id'] = $v['id'];
            $in_data['type'] = "md5";
            $in_data['err_num'] = 0;
            $in_data['passwd'] = md5(substr($v['mobile'],-4));
            if($pa_data){
                Db::name("pay_passwd")->where(['user_id'=>$v['id']])->update($in_data);
            }else{
                Db::name("pay_passwd")->insert($in_data);
            }
        }
        echo "成功";
    }
    /**
     * 同步token
     */
    public function reg_token(){
        $list = Db::name("user")->select();
        foreach ($list as $k=>$v){
            echo $k . PHP_EOL;
            User5g::reg5gUserUp($v); //同步注册5g小店的用户同步token
        }
        echo "成功";
    }
    public function with_save(){
        $list = Db::name("user_withdraw")->select();
        foreach ($list as $k=>$v){
            $with_service = 6;
            $service_money = round($v['money']*$with_service/100,2);
            $yin_money = $v['money']-$service_money;
            Db::name("user_withdraw")->where(['id'=>$v['id']])->update(['service_money'=>$service_money,'with_service'=>$with_service,'yin_money'=>$yin_money]);
        }
    }
    protected function curl_post($url, $uriParam)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
        // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 使用命令不执行
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        // curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($uriParam)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_POSTFIELDS, is_array($uriParam) ? http_build_query($uriParam) : $uriParam); // Post提交的数据包
        // curl_setopt($curl, CURLOPT_POSTFIELDS,$uriParam); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        // curl_setopt($curl, CURLOPT_HTTPHEADER,['Content-type:application/json']); // 设置头部
        curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-type:application/x-www-form-urlencoded']); // 设置头部
        $res = curl_exec($curl); // 执行操作
        $errno = curl_errno($curl);
        curl_close($curl); // 关闭CURL会话
        return $res;
    }
    public function priceadd(){
        $token = '4774a99d67c03sdfwsd73c9f86a4ddd74';
        $cur_token = input('token');
        if ($token != $cur_token) {
            echo 'failed11';
           // return;
        }
        $mobile = request()->post('mobile');
        $price = request()->post('price');
        $memo = request()->post('memo');
        if(!$mobile || !$price){
            $this->error("缺少必传参数");
        }
        $user_data = Db::name("user")->where(['mobile'=>$mobile])->find();
        if(!$user_data){
            $this->error("用户不存在");
        }
        (new ShareUser)->userPriceLog($user_data['id'], $price, 1, $memo,'',[]);
        $this->success("成功");
    }
}
